package com.kelvem.saas.workbanch.ontology.piaoorder.event;

import com.kelvem.saas.workbanch.fsm.anno.Fsm;
import com.kelvem.saas.workbanch.fsm.anno.event.RegisterEvent;
import com.kelvem.saas.workbanch.fsm.anno.state.EventState;
import com.kelvem.saas.workbanch.fsm.anno.state.EventStateGroup;
import com.kelvem.saas.workbanch.fsm.entity.FsmEventReq;
import com.kelvem.saas.workbanch.ontology.address.handle.AddressSignHandle;
import com.kelvem.saas.workbanch.ontology.piaoorder.handle.core.ConvRequest2PiaoOrderHandle;
import com.kelvem.saas.workbanch.ontology.piaoorder.handle.core.PersistencePiaoOrderHandle;
import com.kelvem.saas.workbanch.ontology.piaoorder.handle.core.PiaoOrderStateHandle;
import com.kelvem.saas.workbanch.ontology.piaoorder.handle.core.TestPiaoOrderHandle;
import com.kelvem.saas.workbanch.ontology.piaoorder.handle.create.CreatePiaoNoHandle;
import com.kelvem.saas.workbanch.ontology.piaoorder.model.PiaoOrderEntity;
import com.kelvem.saas.workbanch.ontology.piaoorder.request.PiaoOrderReq;
import lombok.extern.slf4j.Slf4j;

/**
 * @author kelvem
 */
@Slf4j
@Fsm(entityClazz= PiaoOrderEntity.class, projectName="订单系统")
// 正常状态
@EventState(stateCode ="start", stateName ="起始", type="normal", start=1, timeout=600)
@EventState(stateCode ="payment", stateName ="已支付")

@EventState(stateCode ="pickup_pending", stateName ="待取件")
@EventState(stateCode ="pickup_doing", stateName ="取件中")
@EventState(stateCode ="pickup_done", stateName ="已取件")

@EventState(stateCode ="branch_in", stateName ="待揽收")
@EventState(stateCode ="order_confirm", stateName ="已揽收")
@EventState(stateCode ="car_confirm", stateName ="待发车")
@EventState(stateCode ="car_ready", stateName ="已装车")
@EventState(stateCode ="branch_out", stateName ="运输中")

@EventState(stateCode ="trunk_in", stateName ="已到分拨中心")
@EventState(stateCode ="trunk_confirm", stateName ="已分拨")
@EventState(stateCode ="trunk_out", stateName ="已发车")

// 异常状态
@EventState(stateCode ="cancel", stateName ="已取消", type="abnormal", end=1)
// 正常状态组
@EventStateGroup(groupCode ="stage_event_transport", groupName ="可发车", states={"branch_in","payment"})
@EventStateGroup(groupCode ="stage_pickup", groupName ="取货阶段", states={"pickup_pending","pickup_doing","pickup_done"})
@EventStateGroup(groupCode ="stage_branch", groupName ="支线阶段", states={"branch_in","order_confirm","car_confirm","car_ready","branch_out"})
@EventStateGroup(groupCode ="stage_trunk", groupName ="干线阶段", states={"trunk_in","trunk_confirm","trunk_out"})
// 异常状态组
@EventStateGroup(groupCode ="stage_event_cancel", groupName ="可取消", states={"start", "stage_pickup", "stage_branch", "stage_trunk"})
public class PiaoOrderEvent {
    
    @RegisterEvent(eventCode ="PiaoOrder.create", eventName="创建订单", srcState="start", dstState="branch_in",
            beforeHandles = {ConvRequest2PiaoOrderHandle.class},
            handles = {CreatePiaoNoHandle.class, AddressSignHandle.class, PiaoOrderStateHandle.class},
            afterHandles = {PersistencePiaoOrderHandle.class, TestPiaoOrderHandle.class}
    )
    public void create(PiaoOrderReq ordersReq) {
        // do nothing
        log.info("create method do nothing");
    }
    
    @RegisterEvent(eventCode ="PiaoOrder.cancel", eventName="取消订单", srcState="stage_event_cancel", dstState="cancel",
            beforeHandles = {LoadDataHandle.class},
            handles = {PiaoOrderStateHandle.class},
            afterHandles = {PersistencePiaoOrderHandle.class}
    )
    public void cancel(FsmEventReq req) {
        // do nothing
        log.info("confirm method do nothing");
    }
    
    @RegisterEvent(eventCode ="PiaoOrder.confirm", eventName="确认订单", srcState="branch_in", dstState="order_confirm",
            beforeHandles = {LoadDataHandle.class},
            handles = {PiaoOrderStateHandle.class},
            afterHandles = {PersistencePiaoOrderHandle.class}
    )
    public void confirm(FsmEventReq req) {
        // do nothing
        log.info("confirm method do nothing");
    }
    
    @RegisterEvent(eventCode ="PiaoOrder.car_confirm", eventName="支线调车", srcState="branch_in", dstState="car_confirm",
            beforeHandles = {LoadDataHandle.class},
            handles = {PiaoOrderStateHandle.class},
            afterHandles = {PersistencePiaoOrderHandle.class}
    )
    public void carConfirm(FsmEventReq req) {
        // do nothing
        log.info("carConfirm method do nothing");
    }
    
    @RegisterEvent(eventCode ="PiaoOrder.car_ready", eventName="已装车", srcState="car_confirm", dstState="car_ready",
            beforeHandles = {LoadDataHandle.class},
            handles = {PiaoOrderStateHandle.class},
            afterHandles = {PersistencePiaoOrderHandle.class}
    )
    public void carReady(FsmEventReq req) {
        // do nothing
        log.info("carReady method do nothing");
    }
    
    @RegisterEvent(eventCode ="PiaoOrder.branch_transport", eventName="确认订单", srcState="car_ready", dstState="branch_out",
            beforeHandles = {LoadDataHandle.class},
            handles = {PiaoOrderStateHandle.class},
            afterHandles = {PersistencePiaoOrderHandle.class}
    )
    public void branchTransport(FsmEventReq req) {
        // do nothing
        log.info("branch_transport method do nothing");
    }
    
//    @RegisterEvent(eventCode = "PiaoOrder.payment", eventName="支付订单", srcState="confirm", dstState="payment",
//            beforeHandles = {ConvRequest2PiaoOrderHandle.class},
//            handles = {PiaoOrderStateHandle.class},
//            afterHandles = {PersistencePiaoOrderHandle.class}
//    )
//    public void payment(OrderPaymentReq payment) {
//        // do nothing
//        log.info("payment method do nothing");
//    }
//
//    @RegisterEvent(eventCode = "PiaoOrder.transport", eventName="订单发车", srcState="payment", dstState="transport",
//            beforeHandles = {ConvRequest2PiaoOrderHandle.class},
//            handles = {PiaoOrderStateHandle.class},
//            afterHandles = {PersistencePiaoOrderHandle.class}
//    )
//    public void transport(PiaoOrderReq ordersReq) {
//        // do nothing
//        log.info("transport method do nothing");
//    }
}
